#pragma once

class MessageReader
{
public:
	MessageReader(char* pBuffer,uint32 nLength)
	{
		m_pBuffer = pBuffer;
		m_nLength = nLength;
		m_nReadPos = 0;
	}
	
	template<class T>
	bool Read(T& nValue)
	{
		if( m_nReadPos + sizeof(T) <= m_nLength )
		{
			nValue = *((T*)&m_pBuffer[m_nReadPos]);
			m_nReadPos += sizeof(T); 
			return true;
		}
		else
		{
			return false;
		}
	}

	bool ReadBuffer(char* pBuffer,uint32 nLength)
	{
		if( m_nReadPos + nLength <= m_nLength )
		{
			memcpy(pBuffer,&m_pBuffer[m_nReadPos],nLength);
			m_nReadPos += nLength;
			return true;
		}
		else
		{
			return false;
		}
	}

	bool ReadString(std::string& sValue)
	{
		uint32 nLength;
		if ( Read(nLength) )
		{
			sValue.resize(nLength+2);			
			sValue[nLength] = 0;
			sValue[nLength+1] = 0;
			return ReadBuffer((char*)sValue.c_str(),nLength);
		}
		else
		{
			return false;
		}
	}

	char*	Base(){ return &m_pBuffer[m_nReadPos];}
	uint32  Length() {return m_nLength;}
private:
	char*		m_pBuffer;
	uint32		m_nLength;
	uint32		m_nReadPos;
};